//#include "girl.h"
//#include <ddraw.h>
#include "Map.h"
#include "SdlSys.h"

//************************初始化地图数据****************************************
	
short Tile_aqing[10][15] = {//阿青家地图数据
	{ 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8},
	{ 5,65,12,12,12,12,12,12,12,12,12,12,12,63, 9},
	{ 5,64,14,14,14,14,14,14,14,14,14,14,14,62, 9},
	{ 5,10,10,10,10,10,10,14,14,10,10,10,10,10, 9},
	{ 4,11,11,11,11,11,11, 0, 0,11,11,11,11,11, 4},
	{ 4,15,15,15,15,15,15, 0, 0,15,15,15,15,15, 4},
	{ 4,15,15,15,15,15,15, 0, 0,15,15,15,15,15, 4},
	{ 4,15,15,15,15,15,15, 0, 0,15,15,15,15,15, 4},
	{ 2, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 3},
	{15,15,15,15,15,15,15, 0, 0,15,15,15,15,15,15}
	};
short Trap_aqing[10][15] = {//阿青家地图上的陷阱
	{0},//1
	{0},//2
	{0},//3
	{0},//4
	{0},//5
	{0},//6
	{0},//7
	{0},//8
	{0},//9
	{ 0, 0, 0, 0, 0, 0, 0,11}
	};
short Block_aqing[10][15] = {	//阿青家的阻挡布局
	{1,1,1,1,1,1, 1,1,1,1,1, 1,1,1,1},
	{1,1,1,1,1,1, 1,1,1,1,1, 1,1,1,1},
	{1,1,0,0,0,0, 0,0,0,0,0, 0,0,1,1},
	{1,1,1,1,1,1, 1,0,0,1,1, 1,1,1,1},
	{1,1,1,1,1,1, 1,0,0,1,1, 1,1,1,1},
	{1,0,0,0,0,0, 0,0,0,0,0, 0,0,0,1},
	{1,0,0,0,0,0, 0,0,0,0,0, 0,0,0,1},
	{1,0,0,0,0,0, 0,0,0,0,0, 0,0,0,1},
	{1,1,1,1,1,1, 1,0,0,1,1, 1,1,1,1},
	{0,0,0,0,0,0, 0,0,0,0,0, 0,0,0,0}
	};


short Tile_shaoxing[10][15] = {	//越国都城地图
	{17,17,18,24,20,20,20,20,25,17,17,17,18,19,19},
	{17,18,19,30,37,22,22,38,27,17,17,18,24,20,20},
	{21,21,28,20,20,20,20,20,20,29,21,21,21,14,14},
	{35,34,36,26,40,26,26,39,26,33,33,32,33,14,14},
	{14,14,14,26,37,22,22,38,26,33,33,31,33,11,11},
	{10,10,10,26,37,23,23,38,26,12,12,31,12,11,11},
	{16,16,16,16,16,16,16,16,16,16,16,16,16,16,16},
	{16,16,16,16,16,16,16,16,16,16,16,16,16,16,16},
	{16,16,16,16,16,16,16,16,16,16,16,16,16,16,16},
	{16,16,16,16,16,16,16,16,16,16,16,16,16,16,16}
	};
short Trap_shaoxing[10][15] = {	//越国都城地图陷阱
	{0},//1
	{0},//2
	{0},//3
	{0},//4
	{0},//5
	{0},//6
	{12,102,0, 0, 0, 100, 100, 0, 0, 203, 0, 101, 0,   0,0},//7
	{0, 0,  0, 0, 0, 0,   0,   0, 0, 203, 0, 0,   202, 0,0},//8
	{0, 0,  0, 0, 0, 0,   0,   0, 0, 203, 0, 0,   202, 0,10},//9
	{0, 0,  0, 0, 0, 0,   0,   0, 0, 203, 0, 0,   202, 0,0}//10
	};
short Block_shaoxing[10][15] = {	//越国都城阻挡布局
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{1,1,1,1, 1,1,1,1,1, 1,1,1,1,1,1},
	{0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0},
	{0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0},
	{0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0},
	{0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0}
	};

short Tile_citydoor[10][15] = {	//越国城门地图
	{11,11,26,26,41,43,47,44,42,26,26,11,11,11,11},//1
	{11,11,26,26,15,45,47,46,15,26,26,11,11,11,11},//2
	{11,11,26,26,43,47,47,47,44,26,26,11,11,11,11},//3
	{11,11,26,26,45,47,47,47,46,26,26,11,11,11,11},//4*
	{15,48,49,15,54,16,16,16,55,15,15,15,15,15,15},//5
	{48,50,50,49,54,16,16,16,16,16,16,16,16,16,16},//6*
	{50,53,50,51,54,16,16,16,16,16,16,16,16,16,16},//7*
	{50,50,52,49,54,16,16,16,16,16,16,16,16,16,16},//8*
	{50,50,50,51,54,16,16,16,55,15,15,15,15,15,15},//9
	{50,50,50,49,54,16,16,16,15,15,15,15,15,15,15}//10*
	};
short Trap_citydoor[10][15] = {	//越国城门陷阱
	{0},//1
	{0},//2
	{ 0, 0, 0, 0, 0,0,14,0},//3
	{0},//4
	{0},
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0},//6
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,11},//7
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0},//8
	{0},
	{ 0, 0, 0, 0, 0,0,13,0, 0, 0, 0, 0, 0, 0, 0}
	};
short Block_citydoor[10][15] = {	//越国城门阻挡布局
	{1,1,1,1, 1,1,0, 1,1,1,1, 1,1,1,1},
	{1,1,1,1, 1,0,0, 0,1,1,1, 1,1,1,1},
	{1,1,1,1, 1,0,0, 0,1,1,1, 1,1,1,1},
	{1,1,1,1, 0,0,0, 0,0,1,1, 1,1,1,1},
	{0,1,1,0, 0,0,0, 0,0,0,0, 0,0,0,0},
	{1,1,1,1, 0,0,0, 0,0,0,0, 0,0,0,0},
	{1,1,1,1, 0,0,0, 0,0,0,0, 0,0,0,0},
	{1,1,1,1, 0,0,0, 0,0,0,0, 0,0,0,0},
	{1,1,1,1, 0,0,0, 0,0,0,0, 0,0,0,0},
	{1,1,1,1, 0,0,0, 0,0,0,0, 0,0,0,0}
	};

short Tile_fanli[10][15] = {	//范蠡家地图
	{ 6, 7, 7, 7, 7, 7,15,15,15,15,56,20,20,20,57},//1
	{ 5,65,60,60,60,60,16,16,16,16,15,33,32,33,15},//2
	{ 5,64,14,14,14,14,16,16,16,16,15,33,31,33,15},//3
	{ 5,14,14,14,14, 9,58,58,58,16,15,12,31,12,15},//4
	{ 5,10,10,10,10, 9,58,58,58,16,15,15,15,15,15},//5
	{ 5,63,12,12,12, 9,58,58,58,16,16,16,16,16,16},//6*
	{ 5,62,14,14,14,61,58,58,58,16,16,16,16,16,16},//7*
	{ 5,14,14,14,14,14,16,16,16,16,15,15,15,15,15},//8
	{ 5,10,10,10,10,10,15,15,15,15,15,15,15,15,15},//9
	{59,11,11,11,11,11,15,15,15,15,15,15,15,15,15}
	};
short Trap_fanli[10][15] = {	//范蠡家地图陷阱
	{0},//1
	{0},//2
	{0},//3
	{0},//4
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103, 0, 0},//5
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,12},
	{0},
	{0},
	{0}
	};
short Block_fanli[10][15] = {	//范蠡家阻挡布局
	{1,1,1,1,1,1, 0,0,0,0, 1,1,1,1,1},
	{1,1,1,1,1,1, 0,0,0,0, 0,1,1,1,0},
	{1,1,0,0,0,0, 0,0,0,0, 0,1,1,1,0},
	{1,0,0,0,0,1, 1,1,1,0, 0,1,1,1,0},
	{1,1,1,1,1,1, 1,1,1,0, 0,0,0,0,0},
	{1,1,1,1,1,1, 1,1,1,0, 0,0,0,0,0},
	{1,1,0,0,0,1, 1,1,1,0, 0,0,0,0,0},
	{1,0,0,0,0,0, 0,0,0,0, 0,0,0,0,0},
	{1,1,1,1,1,1, 0,0,0,0, 0,0,0,0,0},
	{1,1,1,1,1,1, 0,0,0,0, 0,0,0,0,0}
	};

short Tile_outside[10][15] = {	//郊外地图数据
		{50,50,50,51,67,47,47,69,76,50,50,50,50,50,50},
		{50,50,50,15,66,47,47,47,69,78,79,79,79,79,79},
		{50,50,50,49,15,67,47,47,47,47,47,47,47,47,47},
		{52,50,50,50,72,68,47,47,47,47,47,47,47,47,47},
		{50,50,50,50,71,68,47,47,47,73,74,74,74,74,74},
		{50,52,50,50,71,45,47,47,47,44,75,50,50,50,50},
		{53,50,53,50,70,67,47,47,47,46,76,50,50,50,50},
		{50,50,50,50,49,66,47,47,47,47,44,77,53,50,50},
		{50,52,50,50,50,49,47,47,47,47,46,77,50,50,50},
		{50,50,50,53,50,51,47,47,47,47,47,50,50,52,50}
	};
short Trap_outside[10][15] = { //郊外地图陷阱
		{0},
		{ 0, 0, 0, 0, 0,0,0,0},
		{ 0, 0, 0, 0, 0, 200,200,200, 0, 0, 0, 0, 0, 0,0},
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15},
		{0,0,0,0, 205,205,205,205,205,205},
		{0},
		{0},
		{0},
		{0},
		{ 0, 0, 0, 0, 0, 0,0,12,0,0,0, 0, 0, 0, 0}
	};
short Block_outside[10][15] = {	//郊外地图阻挡布局
		{1,1,1,1, 0,0,0,0,1, 1,1,1,1,1,1},
		{1,1,1,0, 0,0,0,0,0, 1,1,1,1,1,1},
		{1,1,1,1, 0,0,0,0,0, 0,0,0,0,0,0},
		{1,1,1,1, 1,0,0,0,0, 0,0,0,0,0,0},
		{1,1,1,1, 1,0,0,0,0, 0,0,0,0,0,0},
		{1,1,1,1, 1,0,0,0,0, 0,1,1,1,1,1},
		{1,1,1,1, 1,0,0,0,0, 0,1,1,1,1,1},
		{1,1,1,1, 1,0,0,0,0, 0,0,1,1,1,1},
		{1,1,1,1, 1,1,0,0,0, 0,0,1,1,1,1},
		{1,1,1,1, 1,1,0,0,0, 0,0,1,1,1,1}
	};

short Tile_wuguo[10][15] = {	//吴国城门地图数据
	{17,17,17,17,17,13,20,20,20,20,25,17,17,17,17},
	{17,17,17,17,17,17,23,22,22,23,17,17,17,17,17},
	{17,17,17,13,20,20,20,20,20,20,20,20,25,17,17},
	{11,17,11,17,11,22,11,22,11,22,11,23,11,17,11},
	{11,11,11,11,11,11,26,41,42,26,11,11,11,11,11},
	{11,11,11,11,11,11,26,45,46,26,11,11,11,11,11},
	{11,11,11,11,11,11,26,47,47,26,11,11,11,11,11},
	{72,15,15,15,15,15,43,47,47,44,15,15,15,15,15},
	{71,15,15,15,15,15,45,47,47,46,15,15,15,15,15},
	{70,15,15,15,15,43,47,47,47,47,44,15,48,49,15}
};
short Trap_wuguo[10][15] = { //吴国城门地图陷阱
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0,0,0,0,0, 0,0,207,17},
	{0,0,0,0,0, 0,0,207,207},
	{0},
	{0,0,0,0,0, 0,0,201,201,201, 0,0,0,0,0}
};
short Block_wuguo[10][15] = {	//吴国地图阻挡布局
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,0,0,1, 1,1,1,1,1},
	{1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
	{1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
	{1,0,0,0,0, 0,0,0,0,0, 0,0,1,1,0}
};

short Tile_caoyuan[10][15] = {	//草原和宫殿的地图数据
	{	0,	1,	2,	3,	4,	5,	6,	7,	8,	9,	10,	11,	12,	13,	14	},
	{	15,	16,	17,	18,	19,	20,	21,	22,	23,	24,	25,	26,	27,	28,	29	},
	{	30,	31,	32,	33,	34,	35,	36,	37,	38,	39,	40,	41,	42,	43,	44	},
	{	45,	46,	47,	48,	49,	50,	51,	52,	53,	54,	55,	56,	57,	58,	59	},
	{	60,	61,	62,	63,	64,	65,	66,	67,	68,	69,	70,	71,	72,	73,	74	},
	{	75,	76,	77,	78,	79,	80,	81,	82,	83,	84,	85,	86,	87,	88,	89	},
	{	90,	91,	92,	93,	94,	95,	96,	97,	98,	99,	100,101,102,103,104	},
	{	105,106,107,108,109,110,111,112,113,114,115,116,117,118,119	},
	{	120,121,122,123,124,125,126,127,128,129,130,131,132,133,134	},
	{	135,136,137,138,139,140,141,142,143,144,145,146,147,148,149	}
};
short Trap_caoyuan[10][15] = { //草原的地图陷阱
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{206,206,206,206,206,206,206,206,206,206,206,206,206,206,206},
	{0},
	{0},
	{0,0,0,0,0, 0,0,14,0,0, 0,0,0,0,0}
};
short Block_caoyuan[10][15] = {	//草原地图阻挡布局
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1},
	{0},
	{0},
	{0},
	{0},
	{0}
};

short Trap_gongdian[10][15] = { //宫殿的地图陷阱
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{300,300,300,300,300,300,300,300,300,300,300,300,300,300,300},
	{0,0,0,0,0, 0,0,16,0,0, 0,0,0,0,0}
};
short Block_gongdian[10][15] = {	//宫殿地图阻挡布局
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,0,0,1,1, 0,0,1,1,1},
	{0,0,1},
	{0},
	{0},
	{0}
};

short Tile_xiangfang[10][15] = {	//范蠡府厢房的地图数据
	{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
	{65,65,11,11,11,11,65,65,65,11,11,11,11,11,65},
	{64,64,14,14,14,14,64,64,64,14,14,14,14,14,64},
	{14,14,14,14,14,14,14,14,14,14,14,14,14,14,14},
	{14,14,14,14,14,14,14,14,14,14,14,14,14,14,63},
	{14,14,14,14,14,14,14,14,14,14,14,14,14,14,62},
	{14,14,14,14,14,14,14,14,14,14,14,14,14,14,14},
	{10,10,10,10,10,10,14,14,10,10,10,10,10,10,10},
	{33,33,33,33,33,33,14,14,33,33,33,33,33,33,33},
	{60,60,60,60,60,60,14,14,60,60,60,60,60,60,60}
};

short Block_xiangfang[10][15] = { //范蠡府厢房的阻挡布局
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1},
	{1,1,0,0,0, 0,1,1,1,0, 0,0,0,0,1},
	{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
	{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1},
	{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1},
	{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
	{1,1,1,1,1, 1,0,0,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,0,0,1,1, 1,1,1,1,1},
	{1,1,1,1,1, 1,0,0,1,1, 1,1,1,1,1}
};
short Trap_xiangfang[10][15] = { //范蠡府厢房的陷阱分布
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0},
	{0,0,0,0,0,0,0,13}
};

//地图
Map Map_aqing;	//1
Map Map_shaoxing;	//2
Map Map_citydoor;	//3
Map Map_fanli;	//4
Map Map_Wuguo;	//5
Map Map_caoyuan;	//6
Map Map_Gongdian;	//7
Map Map_outside;	//8
Map Map_Xiangfang;	//9

void InitMaps()
{
	//初始化地图	
	Map_aqing.init_map (10,"阿青家",Tile_aqing,Trap_aqing, Block_aqing,map_tile1, 4, message_font, &message_color);	//1
    AddMapIndex(Map_aqing.Name, &Map_aqing);

	Map_shaoxing.init_map (11,"绍兴城",Tile_shaoxing,Trap_shaoxing, Block_shaoxing, map_tile1, 4, message_font, &message_color);	//2
    AddMapIndex(Map_shaoxing.Name, &Map_shaoxing);

	Map_citydoor.init_map (12,"绍兴城门",Tile_citydoor,Trap_citydoor, Block_citydoor,map_tile1, 4, message_font, &message_color);	//3
    AddMapIndex(Map_citydoor.Name, &Map_citydoor);

	Map_fanli.init_map (13,"范蠡府上",Tile_fanli,Trap_fanli, Block_fanli,map_tile1, 4, message_font, &message_color);	//4
    AddMapIndex(Map_fanli.Name, &Map_fanli);

	Map_outside.init_map (14,"郊外",Tile_outside,Trap_outside, Block_outside,map_tile1, 4, message_font, &message_color);	//5
    AddMapIndex(Map_outside.Name, &Map_outside);

	Map_Wuguo.init_map(16, "吴国城门", Tile_wuguo,Trap_wuguo, Block_wuguo, map_tile1, 4, message_font, &message_color);	//6
    AddMapIndex(Map_Wuguo.Name, &Map_Wuguo);

	Map_caoyuan.init_map(15, "放牧草原", Tile_caoyuan, Trap_caoyuan, Block_caoyuan, map_tile2, 15, message_font, &message_color);	//7
    AddMapIndex(Map_caoyuan.Name, &Map_caoyuan);

	Map_Gongdian.init_map(17, "吴王宫", Tile_caoyuan, Trap_gongdian, Block_gongdian, map_tile3, 15, message_font, &message_color);	//8
    AddMapIndex(Map_Gongdian.Name, &Map_Gongdian);

	Map_Xiangfang.init_map(18, "范蠡府东厢房", Tile_xiangfang, Trap_xiangfang,Block_xiangfang,map_tile1,4, message_font, &message_color);	//9
    AddMapIndex(Map_Xiangfang.Name, &Map_Xiangfang);

}

stMapIndex maps[MAP_NUM];

void ClearMapIndex()
{
	for(int i=0; i< MAP_NUM; ++i)
	{
		strcpy(maps[i].szMapName, "");
	}
}

bool AddMapIndex(char* szName, Map *addr)
{
	for(int i=0; i< MAP_NUM; ++i)
	{
		if( !strcmp(maps[i].szMapName, "") )
		{
			strcpy(maps[i].szMapName, szName);
			maps[i].pMap = addr;
			return true;
		}
	}
	return false;
}

Map * GetMapAddr(char* szName)
{
	for(int i=0; i< MAP_NUM; ++i)
	{
		if( !strcmp(maps[i].szMapName, szName) )
			return maps[i].pMap;
	}
	return NULL;
}

//--------------------------------------------------

Map::Map()
{
	short Temp[10][15]={
		{0},{0},{0},{0},{0},
		{0},{0},{0},{0},{0}};
	init_map(0, "",Temp,Temp,Temp,NULL,0, NULL, NULL);
}

Map::~Map()
{
	Npc = NULL;
	Surf = NULL;
}

void Map::init_map (int id, const char name[],short tile[10][15],
				   short trap[10][15],short block[10][15],
				   SDL_Surface *surf, int n,
                   TTF_Font *font, SDL_Color *color)
{
	ID = id;
	strcpy(Name,name);
	for (int i = 0; i<10;i++)
	{
		for(int j=0; j<15; j++)
		{
			Tile[i][j] = tile[i][j];
			Trap[i][j] = trap[i][j];
			Block[i][j] = block[i][j];
		}
	}
    Surf = surf;
	count_in_row = n;
	NpcNum = 0;
	Npc = NULL;
	for(int k=0; k<15; k++)
		Npcs[k] = 0;
    Font =font;
    Color = color;
}

void Map::draw_map_only(SDL_Surface *dest_surf)	//只画地图，用于战斗背景
{
	int i, j;
	int TileNum;
    SDL_Rect map_src, map_dest;

	for(i=0;i<10;i++)
	{
		for(j=0;j<15;j++)
		{
			TileNum = Tile[i][j];
			map_src.x = (TileNum % count_in_row)*32;
			map_src.y = (TileNum/count_in_row)*32;
			map_src.w = 32;
			map_src.h = 32;

            map_dest.x = j*32;
            map_dest.y = i*32;
			map_dest.w = 32;
			map_dest.h = 32;

            SDL_BlitSurface(Surf, &map_src, dest_surf, &map_dest);
		}
	}
}

void Map::draw_map (SDL_Surface * dest_surf, 
				   Role & player, SDL_Surface *assist_surf)
{
	static int flash = 1;
	int i, j;
	int TileNum;
    SDL_Rect map_src, map_dest, assist_src;

	//画地图元素
	for(i=0;i<10;i++)
	{
		for(j=0;j<15;j++)
		{
			TileNum = Tile[i][j];
			map_src.x = (TileNum % count_in_row)*32;
			map_src.y = (TileNum/count_in_row)*32;
			map_src.w = 32;
			map_src.h = 32;

            map_dest.x = j*32;
            map_dest.y = i*32;
			map_dest.w = 32;
			map_dest.h = 32;

            SDL_BlitSurface(Surf, &map_src, dest_surf, &map_dest);
						
			//画场景切换点
			if (Trap[i][j]&& Trap[i][j]<100)
			{
				//SetRect(&Rect_map_dest, j*32, i*32, (j+1)*32, (i+1)*32);
				if (flash > 0) {
                    assist_src.x = 1;
				}
				else {
                    assist_src.x = 36;
				}
                assist_src.y = 176;
                assist_src.w = 35;
                assist_src.h = 35;

                SDL_BlitSurface(assist_surf, &assist_src, dest_surf, &map_dest);
			}

		}
	}
	
	//画Npc
	if (Npc) {
		Role *temp;
		temp = Npc;
		while(temp && (temp->Y < player.Y)) {
			temp->draw_self(dest_surf);
			temp = temp->R;
		}
		player.draw_self(dest_surf);
		while (temp) {
			temp->draw_self(dest_surf);
			temp = temp->R;
		}
	}
	else {
		player.draw_self(dest_surf);
	}
	
    if (!TTF_WasInit()){
        printf("Truetype font engine has not been initialized!\n");
        exit(1);
    }
    SDL_BlitText(Name, dest_surf, SCR_W/2, 30, Font, *Color);

	//更新Flash
	flash = -flash;
}

void Map::add_npc_id(int id)
{
	for(int i=0; i<15; i++) {
		if (Npcs[i] == id)
			return;
	}
	
	for(int i=0; i<15; i++) {
		if (Npcs[i] == 0) {
			Npcs[i] = id;
			return;
		}
	}
}

void Map::del_npc_id(int id)
{
	for(int i=0; i<15; i++) {
		if (Npcs[i] == id)
			Npcs[i] =0;			
	}
}

//往地图中加入NPC
void Map::add_npc(Role * rAdded, int x, int y)
{
	if (NpcNum >= 15) {
        printf("Too many npc in this map!\n");
		return;
	}

	if (npc_in_map(rAdded))
		return;

	rAdded->X = x;
	rAdded->Y = y;
		
	if (!NpcNum) {
		Npc = rAdded;
		rAdded->R = NULL;
		NpcNum++;
		add_npc_id(rAdded->ID);
	}
	else {
		Role * temp = NULL;
		Role *temp1 = NULL;

	    //如果插入的NPC在已有NPC的后方，则排在链表头
		if (rAdded->Y < Npc->Y ) {
			temp = Npc;
			Npc = rAdded;
			rAdded->R = temp;
			NpcNum++;
			add_npc_id(rAdded->ID );
			temp = NULL;
		}
		else {	//将Npc插在队列中间
			temp = Npc;
			while (temp->R) {
				temp1 = temp->R;
				if (rAdded->Y < temp1->Y) {
					temp->R = rAdded;
					rAdded->R = temp1;
					NpcNum++;
					add_npc_id(rAdded->ID );
					temp = NULL;
					temp1 = NULL;
					return;
				}
				temp = temp1;
				temp1 = temp1->R; 
			}
			temp->R = rAdded; //如果不能插在中间就放在尾部
			rAdded->R = NULL;
			NpcNum++;
			add_npc_id(rAdded->ID );
			temp = NULL;
			temp1 = NULL;
		}
	}
}

void Map::del_npc(Role * rDel)	//删除地图中的npc
{
	if (Npc == NULL) {
		return;
	}
	if (Npc == rDel) {
		del_npc_id(rDel->ID );
		Npc = rDel->R;
		NpcNum--;
	}
	else {
		Role *temp = Npc;
		while(temp) {
			if (temp->R == rDel) {
				del_npc_id(rDel->ID );
				temp->R = rDel->R;
				NpcNum--;
				temp = NULL;
				return;
			}
			temp = temp->R;
		}
	}
}

void Map::del_all_npc()
{
	Npc =NULL;
	NpcNum = 0;
	for(int i = 0; i < 15; i++) {
		Npcs[i] = 0;
	}
}

short Map::npc_in_map(Role * r)
{
	Role * temp = Npc;
	while(temp) {
		if(temp == r) {
			temp = NULL;
			return 1;
		}
		temp = temp->R;
	}
	return 0;
}

void Map::del_trap(int x, int y)
{
	int i, j;
	i = y >>5;
	j = x >>5;
	Trap[i][j] = 0;
}

void Map::add_trap(int grid_x, int grid_y, int trap_num)
{
	if(grid_x > 14 || grid_x < 0 || grid_y >9 || grid_y < 0)
		return;
	Trap[grid_y][grid_x] = trap_num;
}

void Map::del_trap_by_num(short num)
{
	for(short i =0; i<10; i++)
	{
		for(short j=0; j< 15; j++)
		{
			if (Trap[i][j] == num)
			{
				Trap[i][j] = 0;
			}
		}
	}
}

short Map::if_block (int xCurrent,int yCurrent)
{
	int i,j;
	i = yCurrent>>5;
	j = xCurrent>>5;
	return Block[i][j];
}

short Map::check_trap (int xCurrent,int yCurrent)
{
	int i,j;
	i = yCurrent>>5;
	j = xCurrent>>5;
	return  Trap[i][j];

}
